import time
import subprocess


def telnet(ip: str, port: int, xinput: str) -> subprocess.Popen:
    proc = subprocess.Popen(
        ['telnet', ip, str(port)],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
    )
    time.sleep(1)
    proc.stdin.write((xinput + '\n').encode('utf8'))
    proc.stdin.flush()
    return proc


if '__main__' == __name__:
    
    xinput = """GET /"""

    proc = telnet('172.22.0.1', 80, xinput)
    print('CREATED.')
    xstdout, xstderr = proc.communicate()
    print('stdout:', xstdout.decode() if xstdout is not None else None)
    print('stderr:', xstderr.decode() if xstderr is not None else None)
    
    time.sleep(5)
    